package com.ztstkj.lzservices.extension

import android.os.SystemClock
import android.view.View

object ViewExtension {

    /**
     * 连续多次点击事件
     * @param clicks 点击次数
     * @param duration 时间间隔
     *
     */
    fun View.setOnClickListenerAfterMultipleClicks(
        clicks: Int = 4,
        duration: Long = 2000,
        action: (view: View) -> Unit
    ) {
       // DebugUtil.warnOut("setOnClickListenerAfterMultipleClicks")
        //DebugUtil.warnOut(this.toString())
        var mHits = LongArray(clicks)
        this.setOnClickListener { view ->
                //DebugUtil.warnOut("点击："+mHits.joinToString(","))
                System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)    //每次点击时，数组向前移动一位
                //DebugUtil.warnOut(mHits.joinToString(","))
                mHits[mHits.size - 1] = SystemClock.uptimeMillis()      //为数组最后一位赋值
                //DebugUtil.warnOut(mHits.joinToString(","))
                if (mHits[0] >= mHits[mHits.size - 1] - duration) {
                    mHits = LongArray(clicks)
                    action(view)
                }

        }
    }
}